package com.iam.api.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.iam.core.base.BaseCommonController;
import com.iam.core.base.BaseCommonService;
import com.iam.core.entities.Result;
import com.iam.core.utils.ResultUtil;
import com.iam.core.utils.SaTokenUtils;
import com.iam.api.model.SysMenu;
import com.iam.api.model.SysRoleMenu;
import com.iam.api.service.SysMenuService;
import com.iam.api.service.SysRoleMenuService;
import jakarta.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <菜单控制器>
 *
 * @author baily
 * @date 2019/7/5
 * @since 1.0
 **/
@RestController
@RequestMapping("/api/sysMenu")
public class SysMenuController extends BaseCommonController<SysMenu> {

    @Autowired
    private SysMenuService<SysMenu> service;

    @Autowired
    private SysRoleMenuService<SysRoleMenu> roleMenuService;

    @Autowired
    private SaTokenUtils saTokenUtils;

    @GetMapping("/me")
    private Result me(HttpServletResponse response) {
        List<String> permits = saTokenUtils.getPermissionList(null);
        if (CollectionUtils.isEmpty(permits)) {
            response.setStatus(401);
            return null;
        }
        return ResultUtil.success(service.list(new QueryWrapper<SysMenu>().in("url", permits).orderByAsc("order_num")));
    }

    @GetMapping("/top")
    private Result top(HttpServletResponse response) {
//        List<String> permits = saTokenUtils.getPermissionList(null);
//        if (CollectionUtils.isEmpty(permits)) {
//            response.setStatus(401);
//            return null;
//        }
//        return ResultUtil.success(service.list(new QueryWrapper<SysMenu>().in("url", permits).orderByAsc("order_num")));
        return ResultUtil.success(service.list(new QueryWrapper<SysMenu>().eq("parent_id", "0")));
    }

    @GetMapping("/sub")
    private Result sub(HttpServletResponse response) {
//        List<String> permits = saTokenUtils.getPermissionList(null);
//        if (CollectionUtils.isEmpty(permits)) {
//            response.setStatus(401);
//            return null;
//        }
        return ResultUtil.success(service.list(new QueryWrapper<SysMenu>().ne("parent_id", "0")));
    }

    /**
     * 获取目标角色的菜单
     *
     * @param roleId
     * @return 返回所有菜单，目标菜单属性check = true
     */
    @GetMapping("/role/{roleId}")
    private Result roleMenu(@PathVariable("roleId") Integer roleId) {
        List<SysMenu> list = service.list();
        List<SysRoleMenu> roleMenus = roleMenuService.list(new QueryWrapper<SysRoleMenu>().eq("role_id", roleId));

        for (SysMenu sysMenu : list) {
            for (SysRoleMenu roleMenu : roleMenus) {
                if (sysMenu.getMenuId().equals(roleMenu.getMenuId())) {
                    sysMenu.setCheck(true);
                    break;
                }
            }
        }
        return ResultUtil.success(list);
    }

    @Override
    public BaseCommonService<SysMenu> getiService() {
        return service;
    }

    @Override
    public String getMouduleName() {
        return "菜单";
    }
}

